//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/interface.js');
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    whose: ["/images/duola2.png", "/images/duola2.png"],
    viewCount: 0,
    viewList: [],
    userId: 0,
    banners: [],
    fucList: []
  },
  onLoad: function() {
    this.get_user_info();
    this.get_banner();
    //this.initMaterial();
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  wdgz: function() {
    wx.navigateTo({
      url: '../wdgz/wdgz?id=' + this.data.userInfo.Id
    })
  },
  grzy: function() {
    wx.navigateTo({
      url: '../grzy/grzy?id=' + this.data.userInfo.Id
    })
  },
  wdfs: function() {
    wx.navigateTo({
      url: '../wdfs/wdfs?id=' + this.data.userInfo.Id
    })
  },
  whosee: function() {
    wx.navigateTo({
      url: '../whosee/whosee?id=' + this.data.userInfo.Id
    })
  },
  bjzl: function() {
    wx.navigateTo({
      url: '../bjzl/bjzl'
    })
  },
  showModal: function() {
    this.bindCall()
  },
  hideModal: function() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  get_user_info: function() {
    var that = this;
    app.Post(api.wxapp.personal_data, {}, function(res) {
      if (res.code == 1) {
        that.setData({
          userInfo: res.data,
        });
        that.initMaterial();

      } else {
        app.WxAlert(res.msg);
      }
    });
  },
  bindCall: function() {
    wx.makePhoneCall({
      phoneNumber: "0519-85808998"
    });
  },
  //获取banan
  get_banner: function() {
    var that = this;
    app.Get(api.wxapp.get_banner, {}, function(res) {
      if (res.code == 1) {
        that.setData({
          banners: res.data.list,
        });
      } else {
        app.WxAlert(res.msg);
      }
    });
  },
  bindurl: function(e) {
    console.log(e);
    wx.navigateTo({
      url: 'webLink?url=' + e.currentTarget.dataset.url,
    })
  },
  initMaterial: function() {
    var that = this;
    console.log(that.data.userInfo);
    var fucList = [];
    // 供货商
    if (that.data.userInfo.CurrentType == 2) {
      fucList = [{
          img: '../../images/wdkz.png',
          title: '我的客资'
        },
        {
          img: '../../images/wsyh.png',
          title: '我是用户'
        },
        {
          img: '../../images/kefu.png',
          title: '联系总部'
        }
      ]
      //   普通用户
    } else {
      fucList = [{
          img: '../../images/guanzhu.png',
          title: '我的关注'
        },
        {
          img: '../../images/fensi.png',
          title: '我的粉丝'
        },
        {
          img: '../../images/bangding.png',
          title: '耳机绑定'
        }, {
          img: '../../images/wddk.png',
          title: '我的打卡'
        },
        {
          img: '../../images/wsdls.png',
          title: '我是代理商'
        },
        {
          img: '../../images/kefu.png',
          title: '联系客服'
        }
      ]
    }
    console.log(fucList)
    this.setData({
      fucList: fucList
    })
  },
  // 根据不同的用户分配不同的点击事件
  swip: function(e) {
    console.log(e.currentTarget.dataset.bind)
    var that = this;
    if (that.data.userInfo.CurrentType == 1) {
      switch (e.currentTarget.dataset.bind) {
        case 0:
          // 我的关注
          that.wdgz()
          break;
        case 1:
          // 我的粉丝
          that.wdfs()
          break;
        case 2:
          // 我的绑定
          if (that.isUserType()) {
            that.binding()
            //    我的客资
          } else {
            that.understrapper()
          }
          break;
        case 3:
          // 我的打卡
          that.grzy()
          break;
        case 4:
          // 用户切
          if (that.data.userInfo.UserType == 3) {
            app.WxRequest(api.wxapp.SwAcount, "GET", {}, function(data) {
              if (data.code == 1) {
                app.writeUserInfo(data.data);
                that.get_user_info();
              }
            });
          } else {
            wx.reLaunch({
              url: '../jihuo/jihuo',
            })
          }
          // that.initMaterial()
          break;
        case 5:
          that.bindCall();
          break;
      }
    } else {
      switch (e.currentTarget.dataset.bind) {
        case 0:
          // 我的关注
          that.wdgz()
          break;
        case 1:
          // 用户切
          if (that.data.userInfo.UserType == 3) {
            app.WxRequest(api.wxapp.SwAcount, "GET", {}, function(data) {
              if (data.code == 1) {
                app.writeUserInfo(data.data);
                that.get_user_info();
              }
            });
          } else {
            wx.reLaunch({
              url: '../jihuo/jihuo',
            })
          }

          break;
        case 2:
          that.bindCall();
          break;
      }

    }
  },
  // 跳转到我的绑定页面
  binding: function() {
    wx.navigateTo({
      url: '../headset/headset'
    })
  },
  // 跳转到我的客资页面
  understrapper: function() {
    if (true) {
      wx.navigateTo({
        url: '../myUser/myUser'
      })
    } else {
      wx.navigateTo({
        url: '../understrapper/understrapper'
      })
    }
  },
  // 判断 用户类型 普通用户 ：true 国货商 false
  isUserType: function() {
    if (app.globalData.userType == 'general') {
      return false
    } else {
      return true
    }
  }
})